<template>
	<view>
		<view class="unlockBox">
			<image mode="widthFix" class="unlockBoxBj" :src="unlockObj.posterImg"></image>
			<view class="unlockBoxBox">
				<view class="unlockBox_main">
					<image v-if="imgSrc" class="welcomeBg" :src="`${imgSrc}/welcomeBg.png`"></image>
					<view class="unlockBox_mainCent">
						<view class="unlockBox_mainTitle">分享好友解锁优惠</view>
						<view class="unlockBox_mainfriend">
							<view class="unlockBox_mainfriendItems" v-for="(item,index) in inviteList" :key="index"
								@click="isShareShow=true">
								<image :src="item.avatar ? item.avatar : '/static/img/userTX.png'"></image>
								<text>{{item.nickname ? item.nickname : `好友${index+1}`}}</text>
							</view>
						</view>
						<view class="unlockBox_stepBox">
							<view class="unlockBox_stepTop">
								<view :class="(nowSchedule>0) ? 'unlockBox_stepItems_act' : 'unlockBox_stepItems'">
									<span :class="nowSchedule > 0 ? 'span_act fistSpan' : 'fistSpan'"></span>
									<!-- <span :class="nowSchedule >= 50 ? 'span_act' : ''"></span> -->
								</view>
								<view :class="(nowSchedule>0) ? 'unlockBox_stepItems_act' : 'unlockBox_stepItems'">
									<span :class="nowSchedule == 100 ? 'span_act' : ''"></span>
								</view>
							</view>
							<view class="unlockBox_stepBotton">
								<view v-for="(item,index) in priceList" :key="index" :class="'unlockBox_stepItemss'">
									<p class="unlockBox_stepItemsP">
										<span>￥</span>
										<span>{{item.price}}</span>
									</p>
									<p>{{item.progressDesc}}</p>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="unlockBotton" v-if="masterId===userId" @click="goPath">
					支付¥{{payPrice}}加入社群
				</view>
			</view>
		</view>

		<!-- 海报 -->
		<!-- <u-overlay :show="isHaibao" @click="isHaibao = false">
			<view class="consultBigBox" @click.stop="">
				<view class="consultBox" @click.stop="">
					<image mode="aspectFill" :show-menu-by-longpress="true" class="consultBoxBg"
						:src="`${imgSrc}/haibao.png`"></image>
				</view>
				<view class="consultBoxButton" @click="baocun">保存海报</view>
				<image class="consultBoxCha" @click.stop="isHaibao = false" src="/static/img/chaIcon.png"></image>
			</view>
		</u-overlay> -->

		<u-popup round="24rpx" mode="bottom" :show="isShareShow" @close="isShareShow=false">
			<view class="unlockBottons">
				<view class="unlockBottonItems">
					<button open-type="share"></button>
					<image src="/static/img/unlockWxIcon.png"></image>
					<text>发送微信好友</text>
				</view>
				<view class="unlockBottonItems" @click="showPoster">
					<image src="/static/img/unlockHbIcon.png"></image>
					<text>生成分享海报</text>
				</view>
			</view>
		</u-popup>

		<canvas class="my-canvas" canvas-id="myCanvas"></canvas>
		<u-popup round="24rpx" mode="center" :show="isShowPoster" @close="isShowPoster=false">
			<view class="poster-wrapper">
				<image :src="posterImg" mode="aspectFit" v-if="posterImg"></image>
				<view class="poster-savebtn" @click="handleSavePoster">保存到相册</view>
			</view>
		</u-popup>

	</view>

</template>

<script>
	import {
		IMG_BASE_URL,
	} from '@/config/app';

	export default {
		components: {},
		data() {
			return {
				payPrice: 0,
				priceList: [],
				masterId: '',
				priceId: '',
				communityId: '',
				unlockObj: null,
				headcount: 0,
				inviteList: [],
				inviteListItems: {
					avatar: "/static/img/unlockAdd.png",
					inviteId: '',
					nickname: ""
				},
				nowSchedule: 0,
				isHaibao: false,
				lists: [],
				isShowPreview: false,
				userId: '',
				isBlack: false,
				place: '',
				// 分享弹窗
				isShareShow: false,
				// 显示邀请海报
				isShowPoster: false,
				// 分享海报
				posterImg: false,
				posterBG: `${IMG_BASE_URL}/haibao.png`
			}
		},
		onLoad(oprtons) {
			if (oprtons.priceId) {
				this.priceId = oprtons.priceId;
				this.communityId = oprtons.communityId;

				this.userId = uni.getStorageSync('user') ? uni.getStorageSync('user').userId : '';

				if (oprtons.masterId) {
					this.isBlack = true;
				} else {
					this.isBlack = false;
				}
				this.masterId = oprtons.masterId ? Number(oprtons.masterId) : this.userId;
			}
			if (oprtons.place) {
				this.place = JSON.parse(oprtons.place)

			} else {
				this.place = JSON.stringify(this.location)
			}

			console.log('this.userId---->', this.place)
			if (this.masterId === this.userId) {
				setTimeout(() => {
					this.initGetInvite();
				}, 1000)
				// {"id":1965,"communityId":1,"benchmarkFree":1,"businessFree":1,"categoryFree":1,"foodFree":1,"siteFree":1,"name":"广州市"}
			} else {
				setTimeout(() => {
					this.getAssist().then(res => {
						this.initGetInviteXq();
					})
				}, 1000)
			}


		},
		onShow() {},
		onShareAppMessage() {
			return {
				title: '加入老谭餐饮情报局看海量餐饮大数据',
				imageUrl: 'https://octopus-wxapp.oss-cn-hangzhou.aliyuncs.com/octopus/yxiao.png',
				path: `/pages/start/index?priceId=${this.priceId}&communityId=${this.communityId}}&masterId=${this.masterId}&place=${this.place}`
			}
		},
		computed: {
			user() {
				return this.$store.state.user;
			},
			imgSrc() {
				return this.$store.state.imgSrc;
			},
			location() {
				return this.$store.state.location;
			},
		},
		methods: {
			// 生成分享海报
			showPoster() {
				uni.showLoading()
				let that = this
				that.isShareShow = false
				if (!that.posterImg) {
					that.$api.getMpcode({
						path: `pages/start/index`,
						scene: `masterId=${that.masterId}&priceId=${that.priceId}`
					}).then(async res => {
						let bgPath = await that.getImage(that.posterBG)
						let mpCode = await that.getImage(res.msg)
						that.drawPoster(bgPath, mpCode)
					})
				} else {
					that.isShowPoster = true
					uni.hideLoading()
				}
			},

			// 保存到手机
			handleSavePoster() {
				uni.showLoading()
				let that = this
				uni.saveImageToPhotosAlbum({
					filePath: that.posterImg,
					success: res => {
						uni.showToast({
							icon: 'success',
							title: "已保存",
							duration: 2000,
						});
					},
					complete: (res) => {
						console.log(res)
						uni.hideLoading()
					}
				})
			},

			//绘制海报
			drawPoster(bgPath, mpCode) {
				uni.showLoading()
				let that = this;
				const ctx = uni.createCanvasContext('myCanvas')
				let WIDTH = 562
				let HEIGHT = 1192
				// 清除画布
				ctx.clearRect(0, 0, WIDTH, HEIGHT);
				// 设置背景
				ctx.drawImage(bgPath, 0, 0, WIDTH, HEIGHT);
				// 小程序码
				ctx.drawImage(mpCode, 28, 1060, 104, 104);
				ctx.setTextBaseline('top')
				ctx.font = "bold 28px Arial";
				ctx.setFillStyle('#333333')
				ctx.fillText('即刻加入社群获取资源', 148, 1074);
				ctx.font = "bold 22px Arial";
				ctx.setFillStyle('#666666')
				ctx.fillText('长按识别进入「章鱼小程序」', 148, 1118);

				ctx.draw(false, res => {
					wx.canvasToTempFilePath({
						canvasId: "myCanvas",
						fileType: "png",
						success: (res) => {
							that.posterImg = res.tempFilePath;
							that.isShowPoster = true
						},
						fail: () => {
							uni.showToast({
								title: "加载失败",
								duration: 2000,
							});
						},
						complete: (res) => {
							uni.hideLoading()
						}
					});
				})
			},

			// 网图本地化
			getImage(path) {
				return new Promise((resolve, reject) => {
					uni.getImageInfo({
						src: path,
						success(res) {
							resolve(res.path)
						},
						fail: (err) => {
							reject(err)
						}
					})
				})
			},

			getAssist() {
				return new Promise((ress, rejj) => {
					let req = {
						masterId: this.masterId,
						priceId: this.priceId
					}
					console.log('req--->', req)
					this.$api.getAssist(req).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								mask: true
							})
							ress();
						} else if (res.code == 503) {
							ress();
						}
					})
				})
			},
			baocun() {
				this.lists = [{
						type: 'image',
						content: `${this.imgSrc}octopus/haibao.png`,
						width: 319,
						height: 558,
						x: 0,
						y: 0,
					},

				]


				this.$nextTick(() => {
					this.$refs.mosoweCanvasComponents.createCanvas();
				});
			},
			_canvasImage(e) {
				uni.saveImageToPhotosAlbum({
					filePath: e,
					success: function() {
						uni.hideLoading();
						uni.showToast({
							title: "保存成功",
							icon: "none"
						});
					},
					fail: function() {
						uni.hideLoading();
						uni.showToast({
							title: "保存失败",
							icon: "none"
						});
					}
				});

			},
			initGetInviteXq() {
				uni.showLoading();
				let req = {
					masterId: this.masterId,
					priceId: this.priceId
				}
				this.$api.getInviteXq(req).then(res => {
					if (res.code == 200) {
						uni.hideLoading();

						this.unlockObj = res.data;
						this.unlockObj.priceList = this.unlockObj.priceList.reverse();
						let priceList = []
						let nowheadcount = 0;
						this.unlockObj.priceList.forEach((item, index) => {
							if (nowheadcount < item.inviteNum) {
								nowheadcount = item.inviteNum;
							}
							if (index) {
								priceList.push(item)
							}
						})
						this.priceList = JSON.parse(JSON.stringify(priceList));
						this.priceList.forEach(item => {
							if (this.headcount < item.inviteNum) {
								this.headcount = item.inviteNum;
							}
						});
						let nowInviteList = [...this.unlockObj.inviteList];
						let nowUnlockNum = nowInviteList.length
						let headNum = nowheadcount - nowUnlockNum;
						for (let i = 0; i < headNum; i++) {
							nowInviteList.push(this.inviteListItems)
						}
						this.inviteList = JSON.parse(JSON.stringify(nowInviteList));



						this.unlockObj.priceList.forEach((item, index) => {
							if (this.unlockObj.inviteList.length >= item.inviteNum) {
								this.payPrice = item.price
							}
						})

						// 进度
						this.nowSchedule = (nowUnlockNum / this.headcount) * 100;




					}
				})
			},
			initGetInvite() {
				uni.showLoading();
				this.$api.getInvite({
					priceId: this.priceId
				}).then(res => {
					if (res.code == 200) {
						uni.hideLoading();
						this.unlockObj = res.data;
						this.unlockObj.priceList = this.unlockObj.priceList.reverse();
						let priceList = []
						let nowheadcount = 0;
						this.unlockObj.priceList.forEach((item, index) => {
							if (nowheadcount < item.inviteNum) {
								nowheadcount = item.inviteNum;
							}
							if (index) {
								priceList.push(item)
							}
						})
						this.priceList = JSON.parse(JSON.stringify(priceList));
						this.priceList.forEach(item => {
							if (this.headcount < item.inviteNum) {
								this.headcount = item.inviteNum;
							}
						});
						let nowInviteList = [...this.unlockObj.inviteList];
						let nowUnlockNum = nowInviteList.length
						let headNum = nowheadcount - nowUnlockNum;
						for (let i = 0; i < headNum; i++) {
							nowInviteList.push(this.inviteListItems)
						}
						this.inviteList = JSON.parse(JSON.stringify(nowInviteList));



						this.unlockObj.priceList.forEach((item, index) => {
							if (this.unlockObj.inviteList.length >= item.inviteNum) {
								this.payPrice = item.price
							}
						})

						// 进度
						this.nowSchedule = (nowUnlockNum / this.headcount) * 100;




					}
				})
			},
			goPath() {
				// this.$store.commit('updatalocation',this.place)
				uni.navigateTo({
					url: `/package/payment/index?buyType=30&communityId=${this.communityId}&priceId=${this.priceId}&informationTitle=分享好友解锁优惠&url=/pages/index/index&isComCode=true`
				});
			},
			goBlack() {
				let that = this;
				if (this.isBlack) {
					let location = uni.getStorageSync('location');
					if (location) {
						this.$api.userLocation({
							city: location.name
						}).then(res => {
							if (res.code == 200) {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}
						})
					} else {

						uni.getFuzzyLocation({
							type: 'gcj02', //返回可以用于uni.openLocation的经纬度
							success: (res) => {
								this.$qqmapsdk.reverseGeocoder({
									//把你的经纬度
									location: {
										latitude: res.latitude,
										longitude: res.longitude
									},
									success(qqmapsdkRes) {
										let city = qqmapsdkRes.result.address_component.city.replace(
											'市', '');
										// 	.replace('市', '');
										that.$api.userLocation({
											city
										}).then(res => {
											if (res.code == 200) {
												uni.setStorageSync('location', res.data
													.location);
												that.$store.commit('updatalocation', res
													.data.location);
												that.$store.commit('updataUser', res.data
													.user);

												this.$api.userLocation({
													city: res.data.location.name
												}).then(res => {
													if (res.code == 200) {
														uni.switchTab({
															url: '/pages/index/index'
														})
													}
												})


											}
										})

									}
								})

							}
						})
					}
				} else {
					uni.navigateBack();
				}

			},
		}
	}
</script>

<style>
	page {
		background-color: #F0F0F0;
	}
</style>
<style lang="scss" scoped>
	.unlockBoxBj {
		width: 100%;
	}

	.unlockBoxBox {
		margin-top: -240rpx;
	}

	.unlockBox_main {
		border-radius: 24rpx;
		margin: 0 32rpx;
		padding: 32rpx;
		box-sizing: border-box;
		position: relative;
		overflow: hidden;
	}

	.unlockBox_mainTitle {
		height: 44rpx;
		font-size: 32rpx;
		font-weight: bold;
		line-height: 44rpx;
		color: #000000;
		text-align: center;

	}

	.unlockBox_mainfriend {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		margin-top: 40rpx;
	}



	.unlockBottons {
		height: 240rpx;
		display: flex;
		background: #fff;
		display: flex;

		.unlockBottonItems {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			position: relative;

			button {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				opacity: 0;
			}

			image {
				width: 80rpx;
				height: 80rpx;
			}

			text {
				font-size: 28rpx;
				font-weight: bold;
				color: #333;
				line-height: 40rpx;
				margin-top: 16rpx;
			}
		}
	}



	.unlockBox_mainfriendItemsTops {
		width: 112rpx;
		height: 112rpx;
		border-radius: 50%;
		border: 3rpx solid #ffda2d;
		background-color: #ffef9d;
		box-sizing: border-box;
		text-align: center;
		line-height: 112rpx;
		font-size: 28rpx;
		font-weight: 550;
	}

	.unlockBox_mainfriendItems {
		flex: 1;
		position: relative;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		overflow: hidden;
		margin: 0 16rpx;

		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
		}

		text {
			display: block;
			width: 100%;
			font-size: 24rpx;
			font-weight: bold;
			line-height: 48rpx;
			color: #333;
			text-align: center;
			border-radius: 10rpx;
			background: #FFDA2D;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			margin-top: -16rpx;
			padding: 0 16rpx;
			box-sizing: border-box;
		}
	}

	.unlockBotton {
		width: 558rpx;
		height: 88rpx;
		line-height: 88rpx;
		background: #FFDA2D;
		border-radius: 24rpx;
		text-align: center;
		font-size: 28rpx;
		font-family: 蘋方-簡;
		font-weight: bold;
		color: #000000;
		margin: 56rpx auto;
	}

	.consultBigBox {
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -60%);
	}

	.consultBox {
		width: 638rpx;
		height: 1116rpx;
		border-radius: 24rpx;
		overflow: hidden;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.consultBoxCha {
		position: absolute;
		left: 50%;
		bottom: calc(-124rpx - 88rpx - 24rpx);
		transform: translateX(-50%);
		width: 96rpx;
		height: 96rpx;
	}

	.consultBoxButton {
		width: 558rpx;
		height: 88rpx;
		line-height: 88rpx;
		background: #FFDA2D;
		opacity: 1;
		border-radius: 24rpx;
		font-size: 28rpx;
		font-family: 蘋方-簡;
		font-weight: bold;
		color: #000000;
		text-align: center;
		position: absolute;
		position: absolute;
		left: 50%;
		bottom: -124rpx;
		transform: translateX(-50%);
	}


	.unlockBox_rule {
		box-sizing: border-box;
		padding: 0 32rpx;
		padding-bottom: 60rpx;

		p {
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 400;
			line-height: 34rpx;
			color: #999999;
			margin-bottom: 3rpx;
		}

		p:nth-child(1) {
			margin-bottom: 8rpx;
		}

	}

	.welcomeBg {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
	}

	.unlockBox_mainCent {
		position: relative;
		width: 100%;
		top: 0%;
		left: 0;
		padding: 24rpx 32rpx;
		box-sizing: border-box;

	}

	.unlockBox_stepTop {
		width: 75%;
		height: 8rpx;
		border-radius: 8rpx;
		background: #f0f0f0;
		display: flex;
		margin-bottom: 25rpx;

	}

	.unlockBox_stepItems {
		flex: 1;
		height: 8rpx;
		background-color: #f0f0f0;
		position: relative;

		span {
			display: inline-block;
			width: 16rpx;
			height: 16rpx;
			background: #f0f0f0;
			border: 2rpx solid #000000;
			border-radius: 50%;
			position: absolute;
			left: 96%;
			top: 4rpx;
			box-sizing: border-box;
			transform: translate(-50%, -50%);
		}

		.span_act {

			background: #ffda2d;
			border: 2rpx solid #000000;

		}

		.fistSpan {
			left: 0;
		}
	}

	.unlockBox_stepItems_act {
		flex: 1;
		height: 8rpx;
		background-color: #ffda2d;
		position: relative;


		span {
			display: inline-block;
			width: 16rpx;
			height: 16rpx;
			background: #f0f0f0;
			border: 2rpx solid #000000;
			border-radius: 50%;
			position: absolute;
			left: 96%;
			top: 4rpx;
			box-sizing: border-box;
			transform: translate(-50%, -50%);
		}

		.span_act {

			background: #ffda2d;
			border: 2rpx solid #000000;

		}

		.fistSpan {
			left: 0;
		}
	}

	.unlockBox_stepBox {
		width: 100%;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		margin-top: 40rpx;
	}

	.unlockBox_stepBotton {
		display: flex;
		width: 100%;
		justify-content: space-between;

	}

	.unlockBox_stepItemss {
		flex: 0 0 25%;

		p {
			text-align: center;
		}

		p:nth-child(2) {
			height: 34rpx;
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 400;
			line-height: 34rpx;
			color: #999999;
		}
	}

	.unlockBox_stepItemsP {
		display: flex;
		justify-content: center;

		span {
			display: inline-block;
			height: 48rpx;
			font-size: 40rpx;
			font-family: DIN;
			font-weight: bold;
			line-height: 48rpx;
			color: #FF471C;
		}

		span:nth-child(1) {
			font-size: 30rpx;
		}

	}

	.my-canvas {
		position: fixed;
		left: 100%;
		width: 562px;
		height: 1192px;
		opacity: 0;
	}

	.poster-wrapper {

		image {
			height: 640rpx;
			margin-top: 32rpx;
		}

		.poster-savebtn {
			color: #333;
			font-size: 28rpx;
			font-weight: bold;
			line-height: 96rpx;
			text-align: center;
			border-radius: 24rpx;
			background-color: #FFDA2D;
			margin: 32rpx;
		}
	}
</style>
